﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.Model;
using LinkCard.Dao;

namespace LinkCard.CustomerView.Controls
{
    public partial class Random5Deals : System.Web.UI.UserControl
    {
        List<ShortDeal> randomDeal = new List<ShortDeal>();
        // int numberOfDealsReturn = 5;
        public long SupplierID = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
            try
            {
                if (Page.RouteData.Values["sid"] != null)
                    SupplierID = Convert.ToInt64(Page.RouteData.Values["sid"]);
            }
            catch (Exception)
            {
            }
            

            if (Deals.Count == 0)
            {
                if (SupplierID > 0)
                    randomDeal = DealDao.GetShortDeal(GetLasted5DealsBySupplierID(SupplierID));
                else
                    randomDeal = DealDao.GetShortDeal(GetLasted5Deals());

            }

            GenerateRandomDealsHTML();
        }

        private void GenerateRandomDealsHTML()
        {
            try
            {
                if (randomDeal == null || randomDeal.Count == 0)
                    return;

                string htmlRandomDeals = "<ul id='slider2'>";

                for (int i = 1; i <= randomDeal.Count; i++)
                {
                    string image = "";
                    var lstImage = Utils.Utilities.GetProductImage(randomDeal[i - 1].Image);
                    if (lstImage != null && lstImage.Count > 0)
                        image = HttpUtility.HtmlDecode(lstImage[0]);

                    htmlRandomDeals += @"
                    <li class='panel'" + i + @">
                        <div class='partner-image'>
                            <a href='"+Utils.Utilities.GetProductDetailUrl(randomDeal[i - 1].DealID,randomDeal[i - 1].Name,randomDeal[i - 1].ParentCategoryId) + @"'>
                                <img src='" + image + @"' alt='' width='366' height='268' />
                            </a>
                        </div>
                        <div class='partner-content'>
                                <h2><a href='" + Utils.Utilities.GetProductDetailUrl(randomDeal[i - 1].DealID, randomDeal[i - 1].Name,randomDeal[i-1].ParentCategoryId) + @"'>" + HttpUtility.HtmlDecode(randomDeal[i - 1].Name) + @"</a></h2>
                               <p>" + HttpUtility.HtmlDecode(randomDeal[i - 1].Description) + @"</p>
                                
                        </div>";

                    #region Button next slide

                    if (i == 1)
                    {
                        htmlRandomDeals += @"
                                <div class='partner-index'>
                                    <ul>
                                        <li><a href='#' class='selected'></a></li>
                                        <li><a href='#'></a></li>
                                        <li><a href='#'></a></li>
                                        <li><a href='#'></a></li>
                                    </ul>
                                </div>";
                    }

                    #endregion

                    htmlRandomDeals += "</li>";
                }

                htmlRandomDeals += "</ul>";

                ltrRandom5Deals.Text = htmlRandomDeals;
            }
            catch (Exception ex)
            {

                throw;
            }
        }

        private List<Model.Deal> GetLasted5Deals()
        {
            List<Model.Deal> _RandomDeals = new List<Model.Deal>();
            try
            {
                Dao.DealDao dealDao = new Dao.DealDao();
                var allDeal = dealDao.GetLatestDeal(5).OrderByDescending(t => t.PostDate).ThenByDescending(t => t.DealID).ToList();

                return allDeal;
            }
            catch (Exception ex)
            {
                // out Message here
                return _RandomDeals;
            }
        }

        private List<Model.Deal> GetLasted5DealsBySupplierID(long sid)
        {
            List<Model.Deal> _RandomDeals = new List<Model. Deal>();
            try
            {
                Dao.DealDao dealDao = new Dao.DealDao();
                var allDeal = dealDao.GetLatestDealBySupplier(sid,5).OrderByDescending(t => t.PostDate).ThenByDescending(t => t.DealID).ToList();

                return allDeal;
            }
            catch (Exception ex)
            {
                // out Message here
                return _RandomDeals;
            }
        }

        private string GetContentFixSize(string content)
        {
            int maxLength = 500;
            string reval = content;

            if (!string.IsNullOrEmpty(content))
            {
                if (content.Length > maxLength)
                    reval = (content.Substring(0, maxLength) + "...");
            }

            return reval;
        }

        public List<ShortDeal> Deals
        {
            get { return randomDeal; }
            set { randomDeal = value; }
        }
    }
}